const User = require('./User');
const Category = require('./Category');
const Product = require('./Product');
const Cart = require('./Cart');
const Order = require('./Order');
const OrderItem = require('./OrderItem');

// 分类与商品关联
Category.hasMany(Product, {
    foreignKey: 'categoryId',
    as: 'products',
    onDelete: 'CASCADE'
});

Product.belongsTo(Category, {
    foreignKey: 'categoryId',
    as: 'category'
});

// 用户与购物车关联
User.hasMany(Cart, {
    foreignKey: 'userId',
    as: 'cartItems',
    onDelete: 'CASCADE'
});

Cart.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user'
});

// 商品与购物车关联
Product.hasMany(Cart, {
    foreignKey: 'productId',
    as: 'cartItems',
    onDelete: 'CASCADE'
});

Cart.belongsTo(Product, {
    foreignKey: 'productId',
    as: 'product'
});

// 用户与订单关联
User.hasMany(Order, {
    foreignKey: 'userId',
    as: 'orders',
    onDelete: 'CASCADE'
});

Order.belongsTo(User, {
    foreignKey: 'userId',
    as: 'user'
});

// 订单与订单项关联
Order.hasMany(OrderItem, {
    foreignKey: 'orderId',
    as: 'orderItems',
    onDelete: 'CASCADE'
});

OrderItem.belongsTo(Order, {
    foreignKey: 'orderId',
    as: 'order'
});

// 商品与订单项关联
Product.hasMany(OrderItem, {
    foreignKey: 'productId',
    as: 'orderItems'
});

OrderItem.belongsTo(Product, {
    foreignKey: 'productId',
    as: 'product'
});

module.exports = {
    User,
    Category,
    Product,
    Cart,
    Order,
    OrderItem
};